/*
 * Process.h
 *
 */

#ifndef PROCESS_H_
#define PROCESS_H_

#include <sys/types.h>
#include <unistd.h>
#include <exception>
#include <sstream>
#include "CStringVector.h"
#include "FdStream.h"

class ProcessException : public std::exception {
	private:
		std::string msg;
	public:
		explicit ProcessException(const std::string& msg) throw();
		virtual ~ProcessException() throw() {}
		const char* what() const throw() { return msg.c_str(); }
};

class Process {
	public:
		Process();
		Process(const std::string& command);
		virtual ~Process();

		void setCommand(const std::string& command);
		void addEnvVar(const std::string& name, const std::string& value);
		void addArgument(const std::string& argument);

		void exe();
		int wait();
		void kill();

		IFdStream& getCout();
		OFdStream& getCin();
	private:
		std::string command;
		pid_t processId;  // id del proceso hijo
		int childStdout[2], childStdin[2];  // pipes al stdout/stdin del hijo
		IFdStream stdcout;
		OFdStream stdcin;
		CStringVector env;  // vars. de entorno para el hijo
		CStringVector args;  // argumentos del hijo
};

#endif /* PROCESS_H_ */
